package com.edu.boot.config;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateDeserializer extends StdDeserializer<Date> {
    
    private static final SimpleDateFormat formatter = new SimpleDateFormat(Constants.DATE_TIME_FORMAT);
    
    public DateDeserializer() {
        this(null);
    }
    
    public DateDeserializer(Class<?> vc) {
        super(vc);
    }
    
    @Override
    public Date deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        String date = p.getText();
        try {
            return formatter.parse(date);
        } catch (Exception e) {
            throw new RuntimeException("日期格式错误：" + date, e);
        }
    }
}
